﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Windows;
using System.Diagnostics;
using System.IO;
using System.Reflection;
using Microsoft.WindowsAPICodePack.Dialogs;

namespace Snip
{
    /// <summary>
    /// Interaction logic for App.xaml
    /// </summary>
    public partial class App : Application
    {
        internal static bool UseCommonDialogForFolderBrowse { get; private set; }

        [STAThread]
        public static void Main(string[] args)
        {
            if (args.Length > 0)
            {
                string sourceFile = args[0];
                TimeSpan splitInterval = TimeSpan.FromMinutes(5);

                SplitOptions options = new SplitOptions();
                options.DestFolder = string.Empty;
                options.SaveInSameFolderAsSourceFile = true;
                options.CreateSubFolders = true;
                options.DeleteSourceFileOnComplete = false;
                options.OpenDestFolderOnComplete = false;
                options.Interval = Snip.Properties.Settings.Default.SplitInterval;

                for (int i = 1; i < args.Length; i++)
                {
                    string a = args[i];
                    if (a.StartsWith("-i:", StringComparison.InvariantCultureIgnoreCase))
                    {
                        uint value;
                        if (uint.TryParse(a.Substring(3), out value))
                        {
                            splitInterval = TimeSpan.FromMinutes(value); ;
                        }
                    }
                }

                ProgressDialog dialog = new ProgressDialog(sourceFile, options);
                dialog.ShowDialog();
            }
            else
            {
                if (System.Environment.OSVersion.Version.Major >= 6)
                {
                    if (File.Exists("Microsoft.WindowsAPICodePack.dll") && File.Exists("Microsoft.WindowsAPICodePack.Shell.dll"))
                    {
                        UseCommonDialogForFolderBrowse = true;
                    }
                    //try
                    //{
                    //    Console.WriteLine(Assembly.LoadFrom("Microsoft.WindowsAPICodePack.dll"));
                    //    Console.WriteLine(Assembly.LoadFrom("Microsoft.WindowsAPICodePack.Shell.dll"));
                    //    WindowsAPICodePackLoaded = true;
                    //}
                    //catch {
                    //    Console.WriteLine("Couldn't load Microsoft.WindowsAPICodePack");
                    //}
                }

                App app = new App();
                app.InitializeComponent();
                app.Run();
            }
        }

        private static SplitOptions _splitOptions;

        public static SplitOptions SplitOptions
        {
            get
            {
                return _splitOptions;
            }
        }

        protected override void OnStartup(StartupEventArgs e)
        {
            _splitOptions = new SplitOptions();

            var settings = Snip.Properties.Settings.Default;
            _splitOptions.Interval = settings.SplitInterval;
            _splitOptions.DestFolder = settings.DestFolder;
            _splitOptions.SaveInSameFolderAsSourceFile = settings.SaveInSameFolderAsSourceFile;
            _splitOptions.CreateSubFolders = settings.CreateSubfolders;
            _splitOptions.OpenDestFolderOnComplete = settings.OpenDestFolderWhenDone;
            _splitOptions.DeleteSourceFileOnComplete = settings.DeleteSourceFileWhenDone;

            base.OnStartup(e);
        }

        protected override void OnExit(ExitEventArgs e)
        {
            var settings = Snip.Properties.Settings.Default;
            settings.SplitInterval = _splitOptions.Interval;
            settings.DestFolder = _splitOptions.DestFolder;
            settings.SaveInSameFolderAsSourceFile = _splitOptions.SaveInSameFolderAsSourceFile;
            settings.CreateSubfolders = _splitOptions.CreateSubFolders;
            settings.OpenDestFolderWhenDone = _splitOptions.OpenDestFolderOnComplete;
            settings.DeleteSourceFileWhenDone = _splitOptions.DeleteSourceFileOnComplete;
            settings.Save();

            base.OnExit(e);
        }

        //internal static FileVersionInfo GetLoadedModuleVersion(string name)
        //{
        //    Process process = Process.GetCurrentProcess();
        //    foreach (ProcessModule module in process.Modules)
        //    {
        //        if (module.ModuleName.ToLower() == name)
        //            return module.FileVersionInfo;
        //    }
        //    return null;
        //}

        //internal static bool VisualStylesEnabled
        //{
        //    get
        //    {
        //        FileVersionInfo verInfo = GetLoadedModuleVersion("comctl32.dll");
        //        return verInfo != null && verInfo.FileMajorPart >= 6;
        //    }
        //}
    }
}
